package com.ywd.materials.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ywd.materials.admin.common.Common;
import com.ywd.materials.admin.context.UserContext;
import com.ywd.materials.admin.dto.MaterialsWarehouseDto;
import com.ywd.materials.admin.dto.query.FirstPageOneQuery;
import com.ywd.materials.admin.dto.query.MaterialsWarehouseOneQuery;
import com.ywd.materials.admin.dto.query.MaterialsWarehouseQuery;
import com.ywd.materials.admin.entity.MaterialsWarehouseEntity;
import com.ywd.materials.admin.entity.MaterialsWarehouseUserEntity;
import com.ywd.materials.admin.exception.CommonException;
import com.ywd.materials.admin.exception.RPage;
import com.ywd.materials.admin.mapper.MaterialsWarehouseMapper;
import com.ywd.materials.admin.utils.TreeWarehouseUtil;
import com.ywd.materials.admin.vo.*;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author:lk
 * @Date: 2025/11/12  00:24
 */
@Slf4j
@AllArgsConstructor
@Service
public class MaterialsWarehouseServiceImpl extends ServiceImpl<MaterialsWarehouseMapper, MaterialsWarehouseEntity> {

    private final MaterialsWarehouseUserServiceImpl materialsWarehouseUserService;


    public List<MaterialsWarehouseTreeVo> getWarehouseTree(MaterialsWarehouseOneQuery query) {
        LambdaQueryWrapper<MaterialsWarehouseEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(MaterialsWarehouseEntity::getDeleted, 0)
                .like(ObjectUtil.isNotEmpty(query.getName()), MaterialsWarehouseEntity::getName, query.getName())
                .eq(ObjectUtil.isNotEmpty(query.getGradeValue()), MaterialsWarehouseEntity::getGradeValue, query.getGradeValue())
                .eq(ObjectUtil.isNotEmpty(query.getStatus()), MaterialsWarehouseEntity::getStatus, query.getStatus());
        List<MaterialsWarehouseEntity> list = this.list(queryWrapper);
        if (CollectionUtils.isEmpty(list)) return null;
        List<MaterialsWarehouseTreeVo> list1 = BeanUtil.copyToList(list, MaterialsWarehouseTreeVo.class);
        return TreeWarehouseUtil.buildWarehouseTree(list1);
    }

    public List<MaterialsWarehouseTreeVo> getWarehouseTreeOne(MaterialsWarehouseOneQuery query) {
        LambdaQueryWrapper<MaterialsWarehouseEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(MaterialsWarehouseEntity::getDeleted, 0)
                .like(ObjectUtil.isNotEmpty(query.getName()), MaterialsWarehouseEntity::getName, query.getName())
                .eq(ObjectUtil.isNotEmpty(query.getGradeValue()) && query.getGradeValue() != "-1", MaterialsWarehouseEntity::getGradeValue, query.getGradeValue())
                .eq(ObjectUtil.isNotEmpty(query.getStatus()), MaterialsWarehouseEntity::getStatus, query.getStatus());
        List<MaterialsWarehouseEntity> list = this.list(queryWrapper);
        if (CollectionUtils.isEmpty(list)) return null;
        List<MaterialsWarehouseTreeVo> list1 = BeanUtil.copyToList(list, MaterialsWarehouseTreeVo.class);
        return TreeWarehouseUtil.buildWarehouseTree(list1);
    }

    public RPage<List<MaterialsWarehouseVo>> getWarehousePage(MaterialsWarehouseQuery query) {
        Page<Object> page = PageHelper.startPage(query.getPage(), query.getLimit());
        List<MaterialsWarehouseVo> list = this.baseMapper.getWarehousePage(query);
        return RPage.ok(list, query.getPage(), query.getLimit(), page.getTotal());
    }

    public List<MaterialsWarehousePartVo> getNextWarehouseList(Long id) {
        return this.baseMapper.getNextWarehouseList(id);
    }

    public MaterialsWarehouseVo getWarehouseDetails(Long id) {
        LambdaQueryWrapper<MaterialsWarehouseEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MaterialsWarehouseEntity::getId, id);
        MaterialsWarehouseEntity one = getOne(queryWrapper);
        return BeanUtil.copyProperties(one, MaterialsWarehouseVo.class);
    }

    public Boolean saveWarehouse(MaterialsWarehouseDto dto) {
        AdminUserVo user = UserContext.getUserOne();
        dto.setCreator(user.getId());
        return save(BeanUtil.copyProperties(dto, MaterialsWarehouseEntity.class));
    }

    public Boolean updateWarehouse(MaterialsWarehouseDto dto) {
        AdminUserVo user = UserContext.getUserOne();
        dto.setUpdater(user.getId());
        return updateById(BeanUtil.copyProperties(dto, MaterialsWarehouseEntity.class));
    }

    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteWarehouse(Long id) {
        //是否关联用户
        MaterialsWarehouseUserEntity warehouseUserEntity = materialsWarehouseUserService.getByWarehouseId(id);
        if (ObjectUtil.isNotEmpty(warehouseUserEntity)) throw new CommonException("该仓库已关联用户, 不能删除");
        return removeById(id);
    }

    public List<Common> getWarehouseListCommon(String name) {
        LambdaQueryWrapper<MaterialsWarehouseEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(MaterialsWarehouseEntity::getDeleted, 0)
                .eq(MaterialsWarehouseEntity::getStatus, 1)
                .like(StringUtils.isNotEmpty(name), MaterialsWarehouseEntity::getName, name);
        List<MaterialsWarehouseEntity> list = this.list(queryWrapper);
        if (CollectionUtils.isEmpty(list)) return null;
        return BeanUtil.copyToList(list, Common.class);
    }


    public RPage<List<Common>> getWarehouseListCommonOne(FirstPageOneQuery query) {
        Page<Object> page = PageHelper.startPage(query.getPage(), query.getLimit());
        LambdaQueryWrapper<MaterialsWarehouseEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(MaterialsWarehouseEntity::getDeleted, 0)
                .eq(MaterialsWarehouseEntity::getStatus, 1)
                .like(StringUtils.isNotEmpty(query.getName()), MaterialsWarehouseEntity::getName, query.getName());
        List<MaterialsWarehouseEntity> list = this.list(queryWrapper);
        if (CollectionUtils.isEmpty(list)) return null;
        List<Common> commons = BeanUtil.copyToList(list, Common.class);
        return RPage.ok(commons, query.getPage(), query.getLimit(), page.getTotal());
    }



}
